﻿using _5E.Infrastructure.Collections;
using StoneAge.Contract;
using StoneAge.Controller.Request;
using StoneAge.Controller.Routing;
using StoneAge.Controller.User;
using StoneAge.DataContract.Messaging;

namespace StoneAge.Controller.Handlers
{
    class LoginHandler : PostResponseRequestHandler
    {
        private IAccountService m_accountService = null;

        public LoginHandler(IAccountService accountService)
            : base(Routes.LOGIN)
        {
            m_accountService = accountService;
        }

        protected override ResponseBase GetResponseBy(WebRequest webRequest)
        {
            LoginRequest request = new LoginRequest();
            request.Name = webRequest.QueryArguments.ExtractFor<string>("name");
            request.Pwd = webRequest.QueryArguments.ExtractFor<string>("pwd");

            LoginResponse response = m_accountService.LoginBy(request);
            if (response.Success)
            {
                UserCookie cookie = new UserCookie
                {
                    AccountId = response.AccountId,
                    LoginName = request.Name
                };
                cookie.Save();
            }
            return response;
        }
    }
}
